<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$typetemplate	= LM::getSessionValueFromRequest( "_{$act}_typetemplate_",	"typetemplate", "");

switch($task){

	case 'quickadd':
	
		$position_name = mosGetParam($_POST, "position_name", "");
		
		if(!empty($typetemplate) && !empty($position_name)){
			$row = LM::getLMTable("Position");
			$row->position_name = $position_name;
			$row->position_projecttype = $typetemplate;
			$row->store();
		} 
		
		LM::redirect("positions");
		
	break;
	
	case 'new':
	case 'edit':
	
		$row = LM::getLMTable("Position");
		
		if($task == "edit"){
			$row->load( $cid[0] );
		}
		
		$template = LM::getTemplate();
		
		$template->assign("view",		"admin/form_positions.tpl.php");
		$template->assign("project",	$project);
		$template->assign("team",		$team);
		$template->assign("my",			$my);
		$template->assign("row",		$row);
		
		$template->display("admin/view.tpl.php");
	break;
	
	case 'save':
		$row = LM::getLMTable("Position");
		$row->save($_POST);
        LM::redirect("positions");
	break;
	
	case 'cancel':
		LM::redirect("positions");
	break;
	
	case "saveorder":
		
		$total				= count( $cid );

		for( $i = 0; $i < $total; $i++ ) {
			
			$position_id	= $database->Quote(intval($cid[$i]));
			$ordering 		= $database->Quote(intval($_POST["ordering"][$i]));
			
			$query = "UPDATE #__leagueman_position SET ordering = $ordering
				   	   WHERE position_id = $position_id";
					   
			$database->setQuery( $query );
			
			if (!$database->query()) {
				echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>\n";
				exit();
			}
		}
		LM::redirect("positions");
	
	break;
	
	default:
		
		$limit		= LM::getSessionValueFromRequest( "_{$act}limit", 'limit', 10 );
        $limitstart = LM::getSessionValueFromRequest( "_{$act}limitstart", 'limitstart',	0 );
		
		$filter 	= "";
		if(!empty($typetemplate)){
			$filter = "WHERE position_projecttype = " . $database->Quote($typetemplate);
		}
		
		$database->setQuery( "SELECT COUNT(position_id) FROM #__leagueman_position $filter");
        $total		= $database->loadResult();
		$pageNav	= LM::getPageNav( $total, $limitstart, $limit, 1 );
		
		$database->setQuery( "SELECT *
                                FROM #__leagueman_position $filter
                            ORDER BY position_projecttype, ordering
                               LIMIT $pageNav->limitstart, $pageNav->limit");
		$rows = $database->loadObjectList();
		
		$template = LM::getTemplate();
		
		$template->assign("view",		"admin/list_positions.tpl.php");
		$template->assign("project",	$project);
		$template->assign("team",		$team);
		$template->assign("rows",		$rows);
		$template->assign("pageNav",	$pageNav);
		$template->assign("my",			$my);
		$template->assign("typetemplate", $typetemplate);
		
		$template->display("admin/view.tpl.php");
	break;
}
	
?>